package com.example.administrator.seppdzxing;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceView;

import com.example.administrator.seppdzxing.camera.CameraManager;


public class MySufaceView extends SurfaceView {

    private final CameraManager cameraManager;

    public MySufaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        cameraManager = new CameraManager(context);
        try {
            cameraManager.openDriver(getHolder());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredHeight = getMeasuredHeight();
        int measuredWidth = getMeasuredWidth();
        if(measuredHeight/measuredWidth>1.8&&measuredHeight>1920&&measuredWidth>=1080){
            float scal = 0;
            try {
                Camera.Size previewSize = cameraManager.getPreviewSize();
                int width = previewSize.width;
                int height = previewSize.height;
                if(width>height){
                    scal = width*1f / height*1f;
                }else {
                    scal = height*1f / width*1f;
                }
            }catch (Exception e){

            }

            if(scal == 0){
                //16/9
                scal = 1.776f;
            }
            float scalWidth = measuredHeight / scal;
            setMeasuredDimension((int) scalWidth,measuredHeight);
        }
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
